﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PEC.Forms
{
    public partial class FrmRtuOption : Form
    {
        PecController _pecController;
        string sErr = "";
        public FrmRtuOption(PecController pec)
        {
            _pecController = pec;
            InitializeComponent();
        }

        private void btTimer_Click(object sender, EventArgs e)
        {
            if (LineID != null)
            {
                frmSchedule frm = new frmSchedule(LineID, _pecController);
                frm.ShowDialog();
            }
        }

        private void cboLine_SelectedValueChanged(object sender, EventArgs e)
        {
            if (!btnCopy)
            {
                if (cboLine.SelectedValue != null && cboLine.SelectedValue.ToString() != "System.Data.DataRowView")
                {
                    LineID = int.Parse(cboLine.SelectedValue.ToString());
                    SetData(LineID);
                }
            }
        }

        private void SetData(int LineID)
        {
            BUS.OpRtuControl ctr = new BUS.OpRtuControl(_pecController.Bus);
            DTO.FilterParameterCollection filters = new DTO.FilterParameterCollection();
            filters.Add("LineId", LineID);
            DTO.OpRtuInfo[] info = ctr.GetByFilter(_pecController.XuongId, filters, ref sErr);
            if (info != null && info.Length > 1)
                txtComId.Text = info[0].ComId.ToString();
            LoadListRTU(LineID);
        }

        private void LoadListRTU(int LineID)
        {
            BUS.OpRtuControl ctr = new BUS.OpRtuControl(_pecController.Bus);
            DTO.FilterParameterCollection filters = new DTO.FilterParameterCollection();
            filters.Add("LineId", LineID);
            filters.Add("ComId", txtComId.Text);
            DataTable dt = ctr.GetByFilterToDataTable(_pecController.XuongId, filters, ref sErr);
            opRtuInfoBindingSource.DataSource = dt;
            dgvList.AllowUserToAddRows = false;
            txtRTUCount.Text = dgvList.RowCount.ToString();
            dgvList.AllowUserToAddRows = true;
        }

        private void FrmRtuOption_Load(object sender, EventArgs e)
        {
            btnCopy = false;
            LoadCBO();
        }

        private void LoadCBO()
        {
            BUS.ChuyenSanXuatControl ctr = new BUS.ChuyenSanXuatControl(_pecController.Bus);
            //cboLine.DisplayMember = "TenChuyen";
            //cboLine.ValueMember = "LineId";
            cboLine.DataSource = ctr.GetByFilterToDataTable(_pecController.XuongId, null, ref sErr);
            
            //cboLine.SelectedValue = "";
            //load column combo
            BUS.OpLenhSXControl ctrlenh = new BUS.OpLenhSXControl(_pecController.Bus);
            colLenhSX.DisplayMember = "Name";
            colLenhSX.ValueMember = "LenhSX";
            colLenhSX.DataSource = ctrlenh.GetByFilterToDataTable(null, ref sErr);
            
            //cboLine.DataSource = ctr.GetByFilterToDataTable(_pecController.XuongId, null, ref sErr);

        }



        private void btSave_Click(object sender, EventArgs e)
        {
            string sErr = "";
            DTO.OpRtuInfo info;
            dgvList.AllowUserToAddRows = false;
            try
            {
                BUS.OpRtuControl ctr = new BUS.OpRtuControl(_pecController.Bus);
                sErr = ctr.Delete(_pecController.XuongId, int.Parse(txtComId.Text), int.Parse(cboLine.SelectedValue.ToString()));
                if (sErr == "")
                {
                    for (int i = 0; i < dgvList.RowCount; i++)
                    {
                        info = GetValue(i);
                        sErr = ctr.InsertUpdate(info);
                    }

                    if (sErr == "")
                    {
                        MessageBox.Show(" Save Successful ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        btnCopy = false;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            dgvList.AllowUserToAddRows = true;
        }

        private DTO.OpRtuInfo GetValue(int i)
        {
            DTO.OpRtuInfo info = new DTO.OpRtuInfo();
            try
            {
                info.XuongId = _pecController.XuongId;
                info.ComId = int.Parse(txtComId.Text);
                info.LineId = int.Parse(cboLine.SelectedValue.ToString());
                info.RtuID = int.Parse(dgvList.Rows[i].Cells["colRtuID"].Value.ToString());
                info.NguoiTH = dgvList.Rows[i].Cells["colNguoiTH"].Value.ToString();
                info.LenhSX = dgvList.Rows[i].Cells["colLenhSX"].Value.ToString();
                info.DonVi = int.Parse(dgvList.Rows[i].Cells["colDonVi"].Value.ToString() == "" ? "0" : dgvList.Rows[i].Cells["colDonVi"].Value.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return info;
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            btnCopy = true;
        }


        public bool btnCopy { get; set; }

        public int LineID { get; set; }

        private void dgvList_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show(" Invalid cell value Error" , "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
